package com.jimmyapp.papa.game.ui;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.View;

import com.jimmyapp.papa.R;
import com.jimmyapp.papa.game.GameState;


public class IntroductionView extends View implements Runnable{
	
	private Bitmap bmap;
	private int offset;
	private Canvas canvas;
	private Paint txtPaint;
	private String ins;
	private int txtLen;
	public int gameCode;
	private int height;
	private int width;
	
	public GameState state = GameState.START;
	public final static int GAMENUM = 2;
	public IntroductionView(Context context, int h, int w) {
		super(context);
		Resources res = this.getResources();
		height = h;
		width =w;
		bmap = BitmapFactory.decodeResource(res, R.drawable.wall);
		txtPaint = new Paint();
		txtPaint.setColor(Color.YELLOW);
		txtPaint.setStyle(Style.FILL_AND_STROKE);
		txtPaint.setTextAlign(Align.CENTER);
		txtPaint.setTextSize(25);
		double r = Math.random();
		if (r<0.5) {
			r = r+0.1;
		}
		Log.i("gameRandowm", r+"s");
		int code = (int)Math.floor((r *  GAMENUM));
		
		String[] instructions = res.getStringArray(R.array.instruction);
		gameCode = code;
		ins = instructions[gameCode];
		txtLen = txtPaint.breakText(ins, true, w-20, null);
		
		canvas = new Canvas();
		
		this.draw(canvas);
		this.setWillNotDraw(false);
		
	}
	private static final String CLICKTEXT = "Click to continue...";
	
	@Override
	public void onDraw(Canvas canvas) {
		if (state.equals(GameState.START)) {
			canvas.drawBitmap(bmap, offset, height/2 - bmap.getHeight(), null);
			canvas.drawText("Instructions",width/2,height/2 - bmap.getHeight()/2, txtPaint);
		} else if (state.equals(GameState.READY)) {
			int y = height/2 - bmap.getHeight()/2-20;
			int drawCount = ins.length()/txtLen +1;
			StringBuffer sb = new StringBuffer(ins);
			while (sb.length() > 0 ) {
				Path path = new Path();
				path.moveTo(15, y);
				path.lineTo(width-15,y);
				y+=30;
				int bindex = sb.substring(0, txtLen).lastIndexOf(" ");
				canvas.drawTextOnPath(sb.substring(0, bindex), path, 0, 1, txtPaint);
				sb.delete(0, bindex);
				if (sb.length() < txtLen) {
					path = new Path();
					path.moveTo(15, y);
					path.lineTo(width-15,y);
					canvas.drawTextOnPath(sb.toString(),path, 0, 1, txtPaint);
					y+=30;
					break;
				}
			}
			Path path3 = new Path();
			path3.moveTo(0, y);
			path3.lineTo(width-10, y);
			canvas.drawTextOnPath(CLICKTEXT,path3,0,1,txtPaint);
			/*
			for(int i =0; i<drawCount;i++) {
				Path path = new Path();
				path.moveTo(15, y);
				path.lineTo(width-15,y);
				int l = Math.min((i+1)*txtLen,ins.length());
				int bindex = ins.substring(i*txtLen, l).lastIndexOf(" ");
				//canvas.drawTextOnPath(ins.substring(i*txtLen,), path, 0, 1, txtPaint);
				
				y+=30;
			}
			
			int bindex = ins.substring(0, txtLen).lastIndexOf(" ");
			canvas.drawTextOnPath(ins.substring(0, bindex), path, 0, 1, txtPaint);
			drawIndex = bindex;
			
			if (txtLen < ins.length()) {
				int bindex = ins.substring(0, txtLen).lastIndexOf(" ");
				Path path2 = new Path();
				path2.moveTo(0, height/2 - bmap.getHeight()/2+10);
				path2.lineTo(width-10, height/2 - bmap.getHeight()/2+10);
				canvas.drawTextOnPath(ins.substring(0, bindex), path, 0, 1, txtPaint);
				canvas.drawTextOnPath(ins.substring(bindex), path2, 0, 1, txtPaint);
				Path path3 = new Path();
				path3.moveTo(0, height/2 - bmap.getHeight()/2+40);
				path3.lineTo(width-10, height/2 - bmap.getHeight()/2+40);
				canvas.drawTextOnPath(CLICKTEXT,path3,0,1,txtPaint);
			} else {
				canvas.drawTextOnPath(ins, path, 0, 1, txtPaint);
				Path path3 = new Path();
				path3.moveTo(0, height/2 - bmap.getHeight()/2+40);
				path3.lineTo(width-10, height/2 - bmap.getHeight()/2+40);
				canvas.drawTextOnPath(CLICKTEXT,path3,0,1,txtPaint);
				
			}*/
			
		}
		
		try {
			Thread.sleep(25);
		} catch (InterruptedException e) {
		}
		
	}

	@Override
	public void run() {
		while(offset <width) {
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			offset += 10;
			this.postInvalidate();
		}
		try {
			Thread.sleep(900);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.postInvalidate();
		bmap.recycle();
		state = GameState.READY;
	}

}
